package com.px.chapter_Dec_1.Gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuiTest extends JFrame implements ActionListener {
    private JTextField textField;
    public GuiTest() {
        setTitle("text计算器");
        setSize(500,600);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel jPanelNo=new JPanel();
        JPanel jPanelX=new JPanel();
        add(jPanelNo, BorderLayout.NORTH);
        add(jPanelX);
        textField=new JTextField();
        JButton clrButton=new JButton("清除");
        jPanelNo.setLayout(new BorderLayout());
        jPanelNo.add(textField);
        jPanelNo.add(clrButton,BorderLayout.EAST);
        clrButton.addActionListener(this);
        String[] btn={"7","8","9","+","4","5","6","-","1","2","3","*","0","(TOT)","/","="};
        jPanelX.setLayout(new GridLayout(4,4));
        for (String string:btn){
            JButton btna=new JButton(string);
            jPanelX.add(btna);
            btna.addActionListener(this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new GuiTest());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    String txt=e.getActionCommand();
    if ("0123456789+-*/".indexOf(txt)>=0){
        textField.setText(textField.getText()+txt);
    }else if (txt.equals("=")){
        String exp=textField.getText();
        System.out.println(Count.Jsq(exp));
    }
    }
}
